package com.example.gobang2.game;

import com.example.gobang2.model.User;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;

@Component
public class RoomManager {
    private ConcurrentHashMap<Integer,Room> userToRoom = new ConcurrentHashMap<>();
    private ConcurrentHashMap<String,Room> rooms = new ConcurrentHashMap<>();

    public Room getRoomById(String roomId){
        if(rooms.get(roomId)==null){
            return null;
        }
        return rooms.get(roomId);
    }

    public void removeRoom(int userId1,int userId2,String roomId){
        rooms.remove(roomId);
        userToRoom.remove(userId1);
        userToRoom.remove(userId2);
    }

    public Room getRoomByUserId(int userId){
        if(userToRoom.get(userId)==null){
            return null;
        }
        return userToRoom.get(userId);
    }

    public void addRoom(int userId1,int userId2,Room room){
        rooms.put(room.getRoomId(),room);
        userToRoom.put(userId1,room);
        userToRoom.put(userId2,room);
    }
}
